package com.learn.grpc.interceptor;

import io.grpc.*;

/**
 * @author zongf
 * @since 2023-07-06
 **/
public class MyServerResponseInterceptor implements ServerInterceptor {
    @Override
    public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata,
                                                                 ServerCallHandler<ReqT, RespT> next) {

        return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(serverCall) {
            @Override
            public void sendMessage(RespT message) {
                super.sendMessage(message);
                String start = metadata.get(Metadata.Key.of("start", Metadata.ASCII_STRING_MARSHALLER));
                Long cost = System.currentTimeMillis() - Long.valueOf(start);
                System.out.println("【MyServerInterceptor】 cost: " + cost + ", 响应数据:" + message);
            }


        }, metadata);
    }
}
